import pygame
import sys
import random

class player_role(object):
    def __init__(self):
        self.step=10 #move 10 px each time, or set 
        #需要8个动作图，上下左右行走x2 尽量用大小一致的方形
        #不同角色可以改变这里的图片
        self.role_all=[pygame.image.load("UP1.png").convert_alpha(),
                         pygame.image.load("UP2.png").convert_alpha(),
                         pygame.image.load("DOWN1.png").convert_alpha(),
                         pygame.image.load("DOWN2.png").convert_alpha(),
                         pygame.image.load("LEFT1.png").convert_alpha(),
                         pygame.image.load("LEFT2.png").convert_alpha(),
                         pygame.image.load("RIGHT1.png").convert_alpha(),
                         pygame.image.load("RIGHT2.png").convert_alpha()
                         ]
        self.role_status=self.role_all[0] 
        self.role_rect=self.role_status.get_rect() #role_status 图片的区域
        self.role_rect.center=(51,51)
        screen.blit(self.role_status,self.role_rect) #窗口上要添加的图片内容，先添加最后层
        pygame.display.update(self.role_rect)#这个很重要，没有会黑屏

    def role_action(self,action):
        Curret_centerx=self.role_rect.centerx
        Curret_centery=self.role_rect.centery
        if action=="UP":
            if self.role_status==self.role_all[0]:
                self.role_status=self.role_all[1]
            else:
                self.role_status=self.role_all[0]
            New_centerx=Curret_centerx
            New_centery=Curret_centery-self.step

        elif action=="DOWN":
            if self.role_status==self.role_all[2]:
                self.role_status=self.role_all[3]
            else:
                self.role_status=self.role_all[2]
            New_centerx=Curret_centerx
            New_centery=Curret_centery+self.step

        elif action=="LEFT":
            if self.role_status==self.role_all[4]:
                self.role_status=self.role_all[5]
            else:
                self.role_status=self.role_all[4]
            New_centerx=Curret_centerx-self.step
            New_centery=Curret_centery
    
        elif action=="RIGHT":
            if self.role_status==self.role_all[6]:
                self.role_status=self.role_all[7]
            else:
                self.role_status=self.role_all[6]
            New_centerx=Curret_centerx+self.step
            New_centery=Curret_centery
        ##困住角色不让他出窗口
        ground_x=back_ground_rect.x
        ground_y=back_ground_rect.y
        if New_centerx>screen.width-50:
            New_centerx=Curret_centerx
            if back_ground_rect.right>screen.width:
                back_ground_rect.x=back_ground_rect.x-self.step
        if New_centery>screen.height-50:
            New_centery=Curret_centery
            if back_ground_rect.bottom>screen.height:
                back_ground_rect.y=back_ground_rect.y-self.step
        if New_centerx<50:
            New_centerx=Curret_centerx
            if back_ground_rect.left<0:
                back_ground_rect.x=back_ground_rect.x+self.step
        if New_centery<50:
            New_centery=Curret_centery
            if back_ground_rect.top<0:
                back_ground_rect.y=back_ground_rect.y+self.step
        self.role_rect.center=(New_centerx,New_centery)
        ###画图######
        screen.blit(back_ground,back_ground_rect)
        #这里需要刷新上所有的通道的位置与背景同步
        Delta_x=back_ground_rect.x-ground_x
        Delta_y=back_ground_rect.y-ground_y
        for count_wall in range(0,Wall.points):
            Wall.Wall_rect[count_wall].centerx=Wall.Wall_rect[count_wall].centerx+Delta_x
            Wall.Wall_rect[count_wall].centery=Wall.Wall_rect[count_wall].centery+Delta_y
            screen.blit(Wall.Wall_status[count_wall],Wall.Wall_rect[count_wall])
            if self.role_rect.colliderect(Wall.Wall_rect[count_wall]):self.role_rect.center=(Curret_centerx+Delta_x,Curret_centery+Delta_y)
        #添加角色图片
        
        screen.blit(self.role_status,self.role_rect) #窗口上要添加的图片内容，先添加最后层
        
        pygame.display.update()
        
class wall_role(object):
    def __init__(self):
        Wall_example=pygame.image.load("Wall1.png")
        Wall_example_rect=Wall_example.get_rect()
        self.points=int(((back_ground_rect.width*back_ground_rect.height)/(Wall_example_rect.width*Wall_example_rect.height))//3)
        self.Wall_status=[]
        self.Wall_rect=[]
        if self.points>0:
            for count in range(0,self.points):
                if count%2==0:
                    self.Wall_status.append(pygame.image.load("Wall1.png"))
                else:
                    self.Wall_status.append(pygame.image.load("Wall2.png"))
                self.Wall_rect.append(self.Wall_status[count].get_rect())
                self.Wall_rect[count].centerx=random.randint(Player.role_rect.width,back_ground_rect.right)
                self.Wall_rect[count].centery=random.randint(Player.role_rect.height,back_ground_rect.bottom)
                    
                if self.Wall_rect[count].right>back_ground_rect.right:self.Wall_rect[count].x=back_ground_rect.right-self.Wall_rect[count].width
                if self.Wall_rect[count].bottom>back_ground_rect.bottom:self.Wall_rect[count].y=back_ground_rect.bottom-self.Wall_rect[count].height
                if self.Wall_rect[count].top<back_ground_rect.top:self.Wall_rect[count].x=back_ground_rect.top
                if self.Wall_rect[count].left<back_ground_rect.left:self.Wall_rect[count].y=back_ground_rect.left
                
                if self.Wall_rect[count].colliderect(Player.role_rect):
                    self.Wall_rect[count].x=self.Wall_rect[count].x+3*Player.role_rect.width
                    self.Wall_rect[count].y=self.Wall_rect[count].y+3*Player.role_rect.height
                
                screen.blit(self.Wall_status[count],self.Wall_rect[count])
                pygame.display.update()
        else:
            print("墙的数量<0.",self.points)
            
        


#主程序如下

if __name__=="__main__":
    pygame.init()
    size=800,800
    screen=pygame.display.set_mode(size, pygame.RESIZABLE)
    screen.fill((255,255,255))
    pygame.display.set_caption("使用方向键进行控制")
    clock=pygame.time.Clock()
    back_ground=pygame.image.load("background.png")
    screen.blit(back_ground,(0,0))
    pygame.display.update()
    back_ground_rect=back_ground.get_rect()
    Player=player_role()
    Wall=wall_role()

    #下边的循环让窗口保持执行下去
    while True:
        clock.tick(15) #循环速度
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.VIDEORESIZE:#设置可以调节窗口大小
                screen = pygame.display.set_mode((event.w, event.h), pygame.RESIZABLE)
## 这是另一种键盘控制的方法，按一下走一下            
##            if event.type == pygame.KEYDOWN:
##                if event.key == pygame.K_UP:
##                    Player.role_action("UP")
##                elif event.key == pygame.K_DOWN:
##                    Player.role_action("DOWN")
##                elif event.key == pygame.K_LEFT:
##                    Player.role_action("LEFT")
##                elif event.key == pygame.K_RIGHT:
##                    Player.role_action("RIGHT")
        Key=pygame.key.get_pressed()
        if Key[pygame.K_UP]:
            Player.role_action("UP")
        elif Key[pygame.K_DOWN]:
            Player.role_action("DOWN")
        elif Key[pygame.K_LEFT]:
            Player.role_action("LEFT")
        elif Key[pygame.K_RIGHT]:
            Player.role_action("RIGHT")










            
